<?php
include("config.php");

if($_POST['submit']){
	/**
	 * --------------------------------
	 *
	 * 获取登录页
	 * 
	 * --------------------------------
	 */
	$snoopy = new Snoopy; 
	//定义POST内容
	$submit_vars["number"]  = $_POST['number'];
	$submit_vars["passwd"]  = $_POST['passwd'];
	$submit_vars["captcha"] = $_POST['captcha'];
	$submit_vars["select"]  = $_POST['select'];
	//模拟HEADER
	$snoopy->agent = $agent;
	$snoopy->host = IP."/reader";
	$snoopy->referer = $verifyurl;
	$snoopy->maxredirs = 0;
	$snoopy->cookies['PHPSESSID'] = $_POST["php_sess_id"];
	//提交
	$snoopy->submit($verifyurl,$submit_vars);
	//解析
	if ($snoopy->status == '302') {
		echo "登录成功！";
	}else{
		if(stripos($snoopy->results,"对不起，密码错误，请查实！")){
	    	echo json_err(3,"用户名或密码错误！");
	    	exit;
		}
		if(stripos($snoopy->results,"wrong check code")){
	    	echo json_err(4,"验证码错误！");
	    	exit;
		}
	}

	/**
	 * --------------------------------
	 *
	 * 获取信息页
	 * 
	 * --------------------------------
	 */
	//获取用户信息
	$snoopy2 = new Snoopy;
	//模拟HEADER
	$snoopy2->agent = $agent;
	$snoopy2->host = IP;
	$snoopy2->referer = $verifyurl;
	$snoopy->expandlinks = true;
	$snoopy2->cookies['PHPSESSID'] = $_POST["php_sess_id"];
	//获取
	$snoopy2->fetch($infourl);
	//解析
	$html = new simple_html_dom();
	$html->load($snoopy2->results);
	echo $html->find('td')[1]->innertext;
	exit;
}


/**
 * --------------------------------
 *
 * 获取验证码和sessin_id
 * 
 * --------------------------------
 */
$snoopy = new Snoopy;
//模拟HEADER
$snoopy->agent = $agent;
$snoopy->host = IP;
//获取
$snoopy->fetch($picurl);
//超时返回
if ($snoopy->status != '200' || $snoopy->timed_out) {
	echo json_err(1,"验证码获取超时！");exit;
}
//解析
$pic_data = "data:image/gif;base64,".base64_encode($snoopy->results);
//设置COOKIE
$snoopy->setcookies(); 
$php_sess_id = $snoopy->cookies['PHPSESSID'];
?>


<!DOCTYPE html>
<html>
<head>
	<title>模拟登录燕大图书馆</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
	<form action="index.php" method="POST" name="form" target="_blank">
		学号：<input type="text" name="number" value="S13085211004">
		密码：<input type="password" name="passwd" value="196371">
		验证码：<input type="text" name="captcha">
		<img src="<?php echo $pic_data; ?>">
		<input type="hidden" name="select" value="cert_no">
		<input type="hidden" name="php_sess_id" value="<?php echo $php_sess_id;?>">
		<input type="submit" name="submit" value="提交">
	</form>
</body>
</html>
